import React from 'react';
import { Router, Switch, Route, Redirect } from 'dva/router';
import dynamic from 'dva/dynamic';

import routes from './routes';

dynamic.setDefaultLoadingComponent(() => {
  return '加载中...';
});

function RouterConfig ({ history, app }) {
  // exact 绝对匹配 不加默认是模糊匹配
  // Switch 包裹的路由，匹配上一个以后就不会匹配其他的
  // Redirect 重定向
  return (
    <Router history={history}>
      <Switch>
        {
          routes.map((item, index) => {
            return (
              <Route
                key={index}
                path={item.path}
                exact
                component={dynamic({app, ...item})}
              />
            );
          })
        }
        <Redirect exact from='/' to='/TodoMVC'/>
        <Route render={() => <div>404  ---  not found</div>} />
      </Switch>
    </Router>
  );
}

export default RouterConfig;